<?php
class brand extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览品牌', '1-2');
		$da = $this -> pub -> get_setting('品牌', 'Brand');
		$q = $this -> db -> get_where('sp_brand_class', array('state' => 1));
		$da['fl'] = $q -> result_array();
		$this -> load -> view('adminx/brand', $da);
	}

	function add() {
		$this -> pub -> logp('创建品牌', '1-2');
		$da['name'] = $this -> input -> post('name');
		$da['fid'] = $this -> input -> post('fid');
		$da['sort'] = $this -> input -> post('sort');
		$da['pic'] = $this -> input -> post('pic');
		$da['pic1'] = $this -> input -> post('pic1');
		$da['logo'] = $this -> input -> post('logo');
		$da['state'] = $this -> input -> post('state');
		$da['bz'] = $this -> input -> post('bz');
		$this -> db -> insert('sp_brand', $da);
		echo json_encode(array('type' => 1));
	}

	function up() {
		$this -> pub -> logp('品牌修改 - ' . $this -> input -> post('idx'), '1-2');
		$da['name'] = $this -> input -> post('name');
		$da['fid'] = $this -> input -> post('fid');
		$da['state'] = $this -> input -> post('state');
		if ($this -> input -> post('pic')) {
			$da['pic'] = $this -> input -> post('pic');
		}
		if ($this -> input -> post('pic1')) {
			$da['pic1'] = $this -> input -> post('pic1');
		}
		if ($this -> input -> post('logo')) {
			$da['logo'] = $this -> input -> post('logo');
		}
		$da['sort'] = $this -> input -> post('sort');
		$da['bz'] = $this -> input -> post('bz');
		$this -> db -> update('sp_brand', $da, array('id' => $this -> input -> post('idx')));
		echo json_encode(array('type' => 1));
	}

	function get_one() {
		$this -> pub -> logp('获取品牌信息 - ' . $this -> input -> post('idx'), '1-2');
		$q = $this -> db -> get_where('sp_brand', array('id' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$da = $q -> row_array(0);
			$da['typex'] = 1;
			echo json_encode($da);
		} else {
			echo json_encode(array('typex' => 2));
		}
	}

	function get() {
		$this -> pub -> logp('获取品牌信息', '1-2');
		if ($this -> input -> post('key')) {
			$this -> db -> like('name', $this -> input -> post('key'));
		}
		$q = $this -> db -> get_where('sp_brand');
		$state[1] = '启用';
		$state[2] = '关闭';
		$da['content'] = '';

		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['id'] . '</td>';
			$da['content'] .= '<td>' . $v['name'] . '</td>';
			$da['content'] .= '<td><img src="' . $this->pub->oss_dz($v['logo']) . '" width="80"></td>';
			$da['content'] .= '<td><img src="' . $this->pub->oss_dz($v['pic']) . '" width="80"></td>';
			$da['content'] .= '<td><img src="' . $this->pub->oss_dz($v['pic1']) . '" width="80"></td>';
			$da['content'] .= '<td>' . $v['sort'] . '</td>';
			$this -> db -> select('name');
			$qq = $this -> db -> get_where('sp_brand_class', array('id' => $v['fid']));
			$ls = $qq -> row_array(0);
			$da['content'] .= '<td>' . $ls['name'] . '</td>';
			$da['content'] .= '<td>' . $state[$v['state']] . '</td>';
			$da['content'] .= '<td>' . $v['bz'] . '</td>';
			$da['content'] .= '<td><button onclick="show_up(\'' . $v['id'] . '\')" type="button"  class="btn default"><i class="fa fa-edit"></i>修改</button></td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

}
?>